package nl.xs4all.dylanschell.deckprinter.transform;

import com.google.inject.Singleton;
import nl.xs4all.dylanschell.deckprinter.model.CardInDeck;
import nl.xs4all.dylanschell.deckprinter.model.CardInSet;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.model.SetDescriptor;

@Singleton
public class ReplaceBasicLandsWithBasicLandsFromSet implements DeckFilter
{
    private final SetDescriptor set;

    public ReplaceBasicLandsWithBasicLandsFromSet(SetDescriptor set)
    {
        this.set = set;
    }

    public DeckDescriptor filter(DeckDescriptor input)
    {
        DeckDescriptor result = new DeckDescriptor(input.getName());
        for (String section : input.getSections())
        {
            for (CardInDeck card : input.getCardsInSection(section))
            {
                if (card.isBasicLand())
                {
                    CardInSet cis = set.getCardByName(card.getName());
                    if (cis != null)
                    {
                        CardInDeck newCard = new CardInDeck(cis.getName(), card.getQuantity(), cis.getId());
                        result.getMain().add(newCard);
                    } else
                    {
                        System.out.println("Unable to replace card" + card.getName());
                        result.getCardsInSection(section).add(card);
                    }
                } else
                {
                    CardInDeck newCard = new CardInDeck(card);
                    result.getCardsInSection(section).add(newCard);
                }
            }
        }
        return result;
    }
}
